﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DBHelper;
using BusinessLayer.RegInfo.Entities;
using System.Data.SqlClient;
using System.Data;


namespace BusinessLayer.RegInfo
{
    public class AdminUserGroupBL
    {
        public List<AdminUserGroup> GetAll()
        {
            var oCommand = new SqlCommand(Common.Constants.StoredProcedures.GetAllAdminUserGroups) { CommandType = CommandType.StoredProcedure };
            var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
            var list = adapter.GetList<AdminUserGroup>(Common.Constants.StoredProcedures.GetAllAdminUserGroups, CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
            return list;
        }
        public int CreateAdminUserGroup(AdminUserGroup group)
        {
            int result = -1;
            try
            {
                var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
                adapter.AddParameter("@Code", group.Code);
                adapter.AddParameter("@Name", group.Name);
                result = adapter.ExecuteNonQuery(Common.Constants.StoredProcedures.InsertAdminUserGroup, CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
                return result;
            }
            catch (Exception ex)
            {

            }
            return result;
        }
        public int AddUserToUserGroup(int userid, int usergroupid)
        {
            int result = -1;
            try
            {
                var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
                adapter.AddParameter("@UserID", userid);
                adapter.AddParameter("@UserGroupID", usergroupid);

                result = adapter.ExecuteNonQuery(Common.Constants.StoredProcedures.InsertAdminUserGroups_User, CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
                return result;
            }
            catch (Exception ex)
            {

            }
            return result;
        }
        public int AddActionToUserGroup(int actionid, int usergroupid)
        {
            int result = -1;
            try
            {
                var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
                adapter.AddParameter("@ActionID", actionid);
                adapter.AddParameter("@UserGroupID", usergroupid);

                result = adapter.ExecuteNonQuery(Common.Constants.StoredProcedures.InsertAdminUserGroups_Action, CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
                return result;
            }
            catch (Exception ex)
            {

            }
            return result;
        }
        public int AddMenuToUserGroup(int menuid, int usergroupid)
        {
            int result = -1;
            try
            {
                var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
                adapter.AddParameter("@MenuID", menuid);
                adapter.AddParameter("@UserGroupID", usergroupid);

                result = adapter.ExecuteNonQuery(Common.Constants.StoredProcedures.InsertAdminUserGroups_Menu, CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
                return result;
            }
            catch (Exception ex)
            {

            }
            return result;
        }
        public int UpdateAdminUserGroup(AdminUserGroup group)
        {
            int result = -1;
            try
            {
                var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
                adapter.AddParameter("@UserGroupID", group.UserGroupID);
                adapter.AddParameter("@Code", group.Code);
                adapter.AddParameter("@Name", group.Name);
                result = adapter.ExecuteNonQuery(Common.Constants.StoredProcedures.UpdateAdminUserGroup, CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
                return result;
            }
            catch (Exception ex)
            {

            }
            return result;
        }

        public AdminUserGroup GetByUserGroupCode(string code)
        {
            var oCommand = new SqlCommand(Common.Constants.StoredProcedures.GetAdminUserGroupByCode) { CommandType = CommandType.StoredProcedure };
            var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
            adapter.AddParameter("@Code", code);
            var list = adapter.GetList<AdminUserGroup>(Common.Constants.StoredProcedures.GetAdminUserGroupByCode, CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
            return list.FirstOrDefault();
        }
        public AdminUserGroup GetByUserGroupID(int id)
        {
            var oCommand = new SqlCommand(Common.Constants.StoredProcedures.GetAdminUserGroupByID) { CommandType = CommandType.StoredProcedure };
            var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
            adapter.AddParameter("@UserGroupID", id);
            var list = adapter.GetList<AdminUserGroup>(Common.Constants.StoredProcedures.GetAdminUserGroupByID, CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
            return list.FirstOrDefault();
        }
        public int DeleteAdminUserGroupByIds(List<string> lstofids)
        {
            int result = -1;

            string ids = "";
            foreach (var id in lstofids)
            {
                ids += id + ",";
            }
            ids = String.IsNullOrEmpty(ids) ? "" : ids.Substring(0,ids.Length-1);
            if (!String.IsNullOrEmpty(ids))
            {
                try
                {
                    var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
                    result = adapter.ExecuteNonQuery(String.Format("DELETE FROM AdminUserGroups WHERE UserGroupID IN ({0});", ids), CommandType.Text, CustomConnectionState.CloseOnExit);
                    return result;
                }
                catch (Exception ex)
                {

                }
            }
            return result;
        }
        
        //sp_AdminUserGroup_Select_By_UserID
        public List<AdminUserGroup> GetAdminUserGroupByUserID(int userid)
        {
            var oCommand = new SqlCommand(Common.Constants.StoredProcedures.GetAdminUserGroupByUserID) { CommandType = CommandType.StoredProcedure };
            var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
            adapter.AddParameter("@UserID", userid);
            var list = adapter.GetList<AdminUserGroup>(Common.Constants.StoredProcedures.GetAdminUserGroupByUserID, CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
            return list;
        }
        //  sp_AdminUserGroup_Select_By_UserName
        public List<AdminUserGroup> GetAdminUserGroupByUserName(string username)
        {
            var oCommand = new SqlCommand(Common.Constants.StoredProcedures.GetAdminUserGroupByUserName) { CommandType = CommandType.StoredProcedure };
            var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
            adapter.AddParameter("@UserName", username);
            var list = adapter.GetList<AdminUserGroup>(Common.Constants.StoredProcedures.GetAdminUserGroupByUserName, CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
            return list;
        }
        #region Delele Privileges

        public int DeleteUserInUserGroup(int userid, int usergroupid)
        {
            int result = -1;
            try
            {
                var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
                adapter.AddParameter("@UserID", userid);
                adapter.AddParameter("@UserGroupID", usergroupid);

                result = adapter.ExecuteNonQuery(Common.Constants.StoredProcedures.DeleteUserInAdminUserGroups, CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
                return result;
            }
            catch (Exception ex)
            {

            }
            return result;
        }
        public int DeleteActionInUserGroup(int actionid, int usergroupid)
        {
            int result = -1;
            try
            {
                var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
                adapter.AddParameter("@ActionID", actionid);
                adapter.AddParameter("@UserGroupID", usergroupid);

                result = adapter.ExecuteNonQuery(Common.Constants.StoredProcedures.DeleteActionInAdminUserGroups, CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
                return result;
            }
            catch (Exception ex)
            {

            }
            return result;
        }
        public int DeleteMenuInUserGroup(int menuid, int usergroupid)
        {
            int result = -1;
            try
            {
                var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
                adapter.AddParameter("@MenuID", menuid);
                adapter.AddParameter("@UserGroupID", usergroupid);

                result = adapter.ExecuteNonQuery(Common.Constants.StoredProcedures.DeleteMenuInAdminUserGroups, CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
                return result;
            }
            catch (Exception ex)
            {

            }
            return result;
        }

        public int DeleteAllUsersInUserGroup(int usergroupid)
        {
            int result = -1;
            try
            {
                var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
                adapter.AddParameter("@UserGroupID", usergroupid);

                result = adapter.ExecuteNonQuery(Common.Constants.StoredProcedures.DeleteAllUsersInAdminUserGroups, CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
                return result;
            }
            catch (Exception ex)
            {

            }
            return result;
        }
        public int DeleteAllActionsInUserGroup( int usergroupid)
        {
            int result = -1;
            try
            {
                var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
                adapter.AddParameter("@UserGroupID", usergroupid);

                result = adapter.ExecuteNonQuery(Common.Constants.StoredProcedures.DeleteAllActionsInAdminUserGroups, CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
                return result;
            }
            catch (Exception ex)
            {

            }
            return result;
        }
        public int DeleteAllMenuInUserGroup( int usergroupid)
        {
            int result = -1;
            try
            {
                var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
                adapter.AddParameter("@UserGroupID", usergroupid);

                result = adapter.ExecuteNonQuery(Common.Constants.StoredProcedures.DeleteAllMenuInMAdminUserGroups, CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
                return result;
            }
            catch (Exception ex)
            {

            }
            return result;
        }

        public int DeleteAllPrivilegesOfUserGroup(int usergroupid)
        {
            int result = -1;
            try
            {
                var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
                adapter.AddParameter("@UserGroupID", usergroupid);

                result = adapter.ExecuteNonQuery(Common.Constants.StoredProcedures.DeleteAllPrivilegesOfAdminUserGroups, CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
                return result;
            }
            catch (Exception ex)
            {

            }
            return result;
        }
        #endregion
    }
}
